﻿namespace HttpClientStudy.UnitTest
{
    public class BaseResultTest
    {
        private readonly ITestOutputHelper _logger;

        public BaseResultTest(ITestOutputHelper outputHelper)
        {
            _logger = outputHelper;
        }

        [Fact]
        public void New_BaseResult_Test()
        {
            var result = new BaseResult() { Code = 1, Message = "成功", Data = null };

            Assert.NotNull(result);
            Assert.True(1 == result.Code);
            Assert.Equal("成功", result.Message);
            Assert.Null(result.Data);
        }

        [Fact]
        public void New_BaseResult_Test2()
        {
            var result = new BaseResult() { Code = 0, Message = "失败", Data = new { Msg = "测试" } };

            Assert.NotNull(result);
            Assert.IsType<BaseResult>(result);
            Assert.Equal(0, result.Code);
            Assert.Equal("失败", result.Message);
        }

        [Fact]
        public void New_BaseResult_Test3()
        {
            var result = new BaseResult() { Code = 0, Message = "异常", Data = new Exception("异常") };

            Assert.NotNull(result);
            Assert.Equal(0, result.Code);
            Assert.Equal("异常", result.Message);
            Assert.IsType<Exception>(result.Data);
        }

        [Fact]
        public void New_Generic_BaseResult_Test()
        {
            var result = new BaseResult<string>() { Code = 1, Message = "成功", Data = string.Empty };

            Assert.NotNull(result);
            Assert.Equal(1, result.Code);
            Assert.Equal("成功", result.Message);
            Assert.Equal(string.Empty, result.Data);
        }

        [Fact]
        public void New_Generic_BaseResult_Create_Test()
        {
            var result = BaseResult<string>.Create(string.Empty);

            Assert.NotNull(result);
            Assert.IsType<string>(result.Data);
            Assert.Equal(0, result.Code);
            Assert.Equal("", result.Message);
            Assert.Empty(result.Data);
        }

        [Fact]
        public void BaseResultUtil_Default_Test()
        {
            var result = BaseResultUtil.Default<string>();
            Assert.NotNull(result);
            Assert.Null(result.Data);
            Assert.Equal(0, result.Code);
            Assert.Equal("", result.Message);

            var result2 = BaseResultUtil.Default<int>();
            Assert.NotNull(result2);
            Assert.IsType<int>(result2.Data);
            Assert.Equal(0,result2.Data);
            Assert.Equal(0, result2.Code);
            Assert.Equal("", result2.Message);
            

            var result3 = BaseResultUtil.Default(new { Name="匿名类"});
            Assert.NotNull(result3);
            Assert.IsType(result3.Data!.GetType(),result3.Data);
            Assert.Equal("匿名类",result3.Data!.Name);
            Assert.Equal(0, result3.Code);
            Assert.Equal("", result3.Message);
        }

        [Fact]
        public void BaseResultUtil_Success_Test()
        {
            var result = BaseResultUtil.Success<string>();
            Assert.NotNull(result);
            Assert.Null(result.Data);
            Assert.Equal(1, result.Code);
            Assert.Equal("成功", result.Message);

            var result2 = BaseResultUtil.Success<int>();
            Assert.NotNull(result2);
            Assert.IsType<int>(result2.Data);
            Assert.Equal(0, result2.Data);
            Assert.Equal(1, result2.Code);
            Assert.Equal("成功", result2.Message);


            var result3 = BaseResultUtil.Success(new { Name = "匿名类" },1,"成功");
            Assert.NotNull(result3);
            Assert.IsType(result3.Data!.GetType(), result3.Data);
            Assert.Equal("匿名类", result3.Data!.Name);
            Assert.Equal(1, result3.Code);
            Assert.Equal("成功", result3.Message);
        }

        [Fact]
        public void BaseResultUtil_Error_Test()
        {
            var result = BaseResultUtil.Error<string>();
            Assert.NotNull(result);
            Assert.Null(result.Data);
            Assert.Equal(0, result.Code);
            Assert.Equal("错误", result.Message);

            var result2 = BaseResultUtil.Error<int>();
            Assert.NotNull(result2);
            Assert.IsType<int>(result2.Data);
            Assert.Equal(0, result2.Data);
            Assert.Equal(0, result2.Code);
            Assert.Equal("错误", result2.Message);

            var result3 = BaseResultUtil.Error(new { Name = "匿名类" }, 0, "错误");
            Assert.NotNull(result3);
            Assert.IsType(result3.Data!.GetType(), result3.Data);
            Assert.Equal("匿名类", result3.Data!.Name);
            Assert.Equal(0, result3.Code);
            Assert.Equal("错误", result3.Message);
        }

        [Fact]
        public void BaseResultUtil_Fail_Test()
        {
            var result = BaseResultUtil.Fail<string>();
            Assert.NotNull(result);
            Assert.Null(result.Data);
            Assert.Equal(0, result.Code);
            Assert.Equal("失败", result.Message);

            var result2 = BaseResultUtil.Fail<int>();
            Assert.NotNull(result2);
            Assert.IsType<int>(result2.Data);
            Assert.Equal(0, result2.Data);
            Assert.Equal(0, result2.Code);
            Assert.Equal("失败", result2.Message);


            var result3 = BaseResultUtil.Fail(new { Name = "匿名类" }, 0, "失败");
            Assert.NotNull(result3);
            Assert.IsType(result3.Data!.GetType(), result3.Data);
            Assert.Equal("匿名类", result3.Data!.Name);
            Assert.Equal(0, result3.Code);
            Assert.Equal("失败", result3.Message);
        }

        [Fact]
        public void BaseResultUtil_Exception_Test()
        {
            var result = BaseResultUtil.Exception<string>(new Exception("测试异常"));
            Assert.NotNull(result);
            Assert.Null(result.Data);
            Assert.Equal(0, result.Code);
            Assert.Contains("异常", result.Message);

            var result2 = BaseResultUtil.Exception<int>(new Exception("测试异常"),0);
            Assert.NotNull(result2);
            Assert.IsType<int>(result2.Data);
            Assert.Equal(0, result2.Data);
            Assert.Equal(0, result2.Code);
            Assert.Contains("异常", result2.Message);
            
            var result3 = BaseResultUtil.Exception(new Exception("测试异常"),new { Name = "匿名类" }, 0);
            Assert.NotNull(result3);
            Assert.IsType(result3.Data!.GetType(), result3.Data);
            Assert.Equal("匿名类", result3.Data!.Name);
            Assert.Equal(0, result3.Code);
            Assert.Contains("异常", result3.Message);
        }
    }
}
